package factory.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import databaseManageInfo.dto.DatabaseAddDTO;
import factory.mapper.DatabaseManageMapper;
import factory.service.DatabaseManageService;
import databaseManageInfo.domain.DatabaseManageInfo;
import databaseManageInfo.dto.DatabaseManageDTO;
import databaseManageInfo.vo.DatabaseManageInfoVO;
import org.springframework.stereotype.Service;
import result.PageDTO;
import utils.BeanConverter;

import java.time.LocalDateTime;
import java.util.Date;

@Service
public class DatabaseManageServiceImpl extends ServiceImpl<DatabaseManageMapper, DatabaseManageInfo>
                                       implements DatabaseManageService {


    @Override
    public Page<DatabaseManageInfoVO> pageAll(PageDTO pageDTO) {
        Page<DatabaseManageInfo> page = new Page<>(pageDTO.getPageNumber(), pageDTO.getPageSize());
        Page<DatabaseManageInfo> pageResult = this.page(page);
        Page<DatabaseManageInfoVO> returnPage = new Page<>();
        returnPage.setRecords(BeanConverter.convertList(pageResult.getRecords(),DatabaseManageInfoVO.class));
        returnPage.setCurrent(pageResult.getCurrent());
        returnPage.setTotal(pageResult.getTotal());
        returnPage.setSize(pageResult.getSize());
        return returnPage;
    }

    @Override
    public Page<DatabaseManageInfoVO> pageAllByQuery(DatabaseManageDTO databaseManageDTO) {
        Page<DatabaseManageInfo> page = new Page<>(databaseManageDTO.getPageNumber(), databaseManageDTO.getPageSize());
        LambdaQueryWrapper<DatabaseManageInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DatabaseManageInfo::getStatus,databaseManageDTO.getStatus())
                .like(DatabaseManageInfo::getDatabaseName,databaseManageDTO.getDatabaseName());
        Page<DatabaseManageInfo> resultPage = this.page(page, wrapper);
        Page<DatabaseManageInfoVO> returnPage = new Page<>();
        returnPage.setRecords(BeanConverter.convertList(resultPage.getRecords(),DatabaseManageInfoVO.class));
        returnPage.setCurrent(resultPage.getCurrent());
        returnPage.setTotal(resultPage.getTotal());
        returnPage.setSize(resultPage.getSize());
        return returnPage;

    }

    @Override
    public Boolean addDatabase(DatabaseAddDTO databaseAddDTO) {
        DatabaseManageInfo databaseManageInfo = new DatabaseManageInfo();
        databaseManageInfo.setCreateTime(LocalDateTime.now());
        databaseManageInfo.setUpdateTime(LocalDateTime.now());
        databaseManageInfo.setStatus(0);
        databaseManageInfo.setDatabaseName(databaseAddDTO.getDatabaseName());
        databaseManageInfo.setDatabaseDesc(databaseAddDTO.getDatabaseDesc());
        databaseManageInfo.setDatabaseType(databaseAddDTO.getDatabaseType());
        databaseManageInfo.setUrl(databaseAddDTO.getUrl());
        databaseManageInfo.setUser(databaseAddDTO.getUser());
        databaseManageInfo.setPassword(databaseAddDTO.getPassword());
        if(this.save(databaseManageInfo)){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean updateDatabase(Integer id,DatabaseAddDTO databaseAddDTO) {
        DatabaseManageInfo thisBaseDate = this.getById(id);
        DatabaseManageInfo databaseManageInfo = new DatabaseManageInfo();
        databaseManageInfo.setId(id);
        databaseManageInfo.setDatabaseName(databaseAddDTO.getDatabaseName());
        databaseManageInfo.setDatabaseDesc(databaseAddDTO.getDatabaseDesc());
        databaseManageInfo.setDatabaseType(databaseAddDTO.getDatabaseType());
        databaseManageInfo.setUrl(databaseAddDTO.getUrl());
        databaseManageInfo.setPassword(thisBaseDate.getPassword());
        databaseManageInfo.setUser(thisBaseDate.getUser());
        databaseManageInfo.setStatus(thisBaseDate.getStatus());
        databaseManageInfo.setUpdateTime(LocalDateTime.now());
        databaseManageInfo.setCreateTime(thisBaseDate.getCreateTime());
        if(this.updateById(databaseManageInfo)){
            return true;
        }else{
            return false;
        }

    }

    @Override
    public Boolean updateStatic(Integer id) {
        DatabaseManageInfo thisDatabase = this.getById(id);
        switch (thisDatabase.getStatus()){
            case 0, 2:
                thisDatabase.setStatus(1);break;
            case 1:
                thisDatabase.setStatus(2);break;
        }
        if(this.updateById(thisDatabase)){
            return true;
        }else {
            return false;
        }
    }
}
